import axios from 'axios';

import { getToken,setToken } from './token';

//import.meta.env.VITE_APP_API_BASEURL

import router from '@/router';
const request = axios.create({
    baseURL: '/api/',
    withCredentials: false,// 用于配置请求接口跨域时是否需要凭证
    timeout: 30000 //
})

axios.defaults.headers['Content-Type'] = 'application/json?chatset=utf-8'

request.interceptors.request.use((config) => {
    if(getToken('vividToken')!==null){
        config.headers['Authorization'] = getToken('vividToken')
    }
    return config;

    },(error) => {
    // 发生异常
    console.log("请求异常====》",error);
    return Promise.reject(error);
})

request.interceptors.response.use((response) => {
    let {msg,code} = response.data
    console.log("code=====>",code,'msg====>',msg);
    if(code == null) {
        return response;
    }else if(code == 200) {
        return response;
    }else if(code == 500) {
        ElMessage.error(msg);
    }else if(code == 401) {
        ElMessage.error('没有操作权限！');
    }
})

export default request;